<!doctype html>
<html>
<head>
</head>
<body>
  <div class="rt-player"  id="rt-player001"></div><!-- end rt-player -->


  <button onclick="play2()">play talk</button>
  <button onclick="stop2()">stop</button>
  <button onclick="play3()">play monitor</button>
  <button onclick="pause()">start recording</button>
  <button onclick="resume()">stop recording</button>
  <script src="aRecorder.js"></script>
  <!--<script src="libacodec.js"></script>-->
  <script src="pcm-player.js"></script>
  <script src="webaudio.js"></script>

  <script>
    (function(){
      var websocket = null;
      var nsession = null;
      function login(){
		      websocket.send(JSON.stringify({
                        "messageID": 101,
                        "nLength": 11,
                        "nQueue": 1,
                        "nParameter1": 11,
                        "nParameter2": 12,
                        "webstring": "abcefghijk",
                        "szUserName": "super",
                        "szUserPassword": "super"
          }))
	   }

    var websocketHandler = {
      onmessage:function(evt){
        var response = JSON.parse(evt.data)
         console.info(response)
          switch(response.messageID){
                    case 102 :{
                         var deviceID =  "90001";
                         //var deviceID = "00116";
                         //var deviceID = "1000087";
                         //var audiotype = "aac";
                         var audiotype = "g711";
                         var channel = 1;
                         var sessionKey = response.nSessionID;
                         console.info("response",response);
                         console.info("deviceid",deviceID,sessionKey);
						 var listener = {
						     onPlaySuccess:function(){

							 }
						 }
                         if(index == 0){
                          webaudio.playTalk(audiotype,[deviceID],sessionKey,null);
                         }else{
                          webaudio.playMonitor(audiotype,[deviceID],sessionKey,null);
                         }
                        //
                        break;
             }
          }
      }
    };

     function startWebsocket(url,callback){
			websocket = new WebSocket(url);
			websocket.binaryType = 'arraybuffer';

			websocket.onopen = function(evt) {
			    console.info("websocket onopened")
			    login()
			};

			websocket.onerror = function(evt) {
			    console.info("websocket onerror "+evt.data)
			}

			websocket.onclose = function(){
				console.info("websocket onclose ")
			}
			websocket.onmessage = callback.onmessage
		}
    //var url ="ws://192.168.1.10:6006";
    var url ="ws://192.168.1.10:6006";
    //var url ="ws://118.89.61.175:6006";
    var webaudio = null;
    /*
    var canvas = document.getElementById("playCanvas");
    var timeTrack = document.getElementById("timeTracker");
    var timeLabel = document.getElementById('currentTime');
    var durationLabel = document.getElementById('totalTime');*/



    var option = {
      IOnBtnStopEvent:function(data){
        console.info("IOnBtnStopEvent",data);
      },
      url:url,
      width:"900px",
      height:"450px",
      container:document.getElementById("rt-player001"),
    }
    var index = 0;
    webaudio = new WebAudio({});
    window.play2 = function(){
      index = 0;
      startWebsocket(url,websocketHandler);
    }

    window.stop2 = function(){
      webaudio.stop();
    }

    window.play3 = function(){
      index = 1;
      startWebsocket(url,websocketHandler);
    }

    window.pause = function(){
      webaudio.startRecording();
    }

    window.resume = function(){
      webaudio.stopRecording();
    }


    })(window)

  </script>
</body>
</html>
